{% extends 'base.html' %}
{% load static %}

{% block title %}我的兑换 - 活动社交平台{% endblock %}
{% if debug %}
<div class="alert alert-info">
    <h6>调试信息：</h6>
    <p>用户: {{ user.username }}</p>
    <p>总兑换记录数: {{ page_obj.paginator.count }}</p>
    <p>当前页记录数: {{ page_obj|length }}</p>
</div>
{% endif %}
{% block content %}
<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h4>我的兑换记录</h4>
                <div>
                    <a href="{% url 'points_mall' %}" class="btn btn-outline-primary">
                        <i class="fas fa-arrow-left"></i> 返回商城
                    </a>
                    <a href="{% url 'points_history' %}" class="btn btn-outline-info ms-2">
                        <i class="fas fa-history"></i> 积分历史
                    </a>
                </div>
            </div>

            <!-- 兑换记录列表 -->
            <div class="card">
                <div class="card-body">
                    {% if page_obj %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    
                                    <th>商品信息</th>
                                    <th>兑换时间</th>
                                    <th>消耗积分</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for exchange in page_obj %}
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            {% if exchange.product.image %}
                                            <img src="{{ exchange.product.image.url }}"
                                                 class="me-3"
                                                 style="width: 50px; height: 50px; object-fit: cover; border-radius: 8px;"
                                                 alt="{{ exchange.product.name }}">
                                            {% endif %}
                                            <div>
                                                <strong>{{ exchange.product.name }}</strong>
                                                <br>
                                                <small class="text-muted">{{ exchange.product.description|truncatewords:10 }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>{{ exchange.created_at|date:"Y-m-d H:i" }}</td>
                                    <td>
                                        <span class="text-danger">-{{ exchange.points_used }}</span>
                                    </td>
                                    <td>
                                        <span class="badge
                                            {% if exchange.status == 'completed' %}bg-success
                                            {% elif exchange.status == 'shipped' %}bg-info
                                            {% elif exchange.status == 'pending' %}bg-warning
                                            {% else %}bg-danger{% endif %}">
                                            {{ exchange.get_status_display }}
                                        </span>
                                        {% if exchange.exchange_code %}
                                        <br>
                                        <small class="text-muted">兑换码: {{ exchange.exchange_code }}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if exchange.status == 'pending' and exchange.product.category == 'physical' %}
                                        <button class="btn btn-sm btn-outline-primary"
                                                onclick="editShippingInfo({{ exchange.id }})">
                                            修改地址
                                        </button>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- 分页 -->
                    {% if page_obj.has_other_pages %}
                    <nav aria-label="Page navigation" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}">上一页</a>
                            </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                <li class="page-item active">
                                    <span class="page-link">{{ num }}</span>
                                </li>
                                {% else %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                                </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.next_page_number }}">下一页</a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}

                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-gift fa-3x text-muted mb-3"></i>
                        <p class="text-muted">暂无兑换记录</p>
                        <a href="{% url 'points_mall' %}" class="btn btn-primary">去兑换商品</a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 修改地址模态框 -->
<div class="modal fade" id="editShippingModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">修改收货信息</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="editShippingForm">
                    <input type="hidden" id="exchangeId" name="exchange_id">
                    <div class="mb-3">
                        <label class="form-label">收货地址</label>
                        <textarea class="form-control" id="editShippingAddress" name="shipping_address" rows="3" required></textarea>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">联系人</label>
                            <input type="text" class="form-control" id="editContactName" name="contact_name" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">联系电话</label>
                            <input type="tel" class="form-control" id="editContactPhone" name="contact_phone" required>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="saveShippingInfo">保存</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function editShippingInfo(exchangeId) {
    // 这里可以实现修改收货信息的功能
    $('#exchangeId').val(exchangeId);
    $('#editShippingModal').modal('show');
}

$('#saveShippingInfo').click(function() {
    const exchangeId = $('#exchangeId').val();
    const shippingAddress = $('#editShippingAddress').val();
    const contactName = $('#editContactName').val();
    const contactPhone = $('#editContactPhone').val();

    if (!shippingAddress || !contactName || !contactPhone) {
        alert('请填写完整的收货信息');
        return;
    }

    // 发送AJAX请求更新收货信息
    $.ajax({
        url: '/user/update_shipping_info/',
        type: 'POST',
        data: {
            exchange_id: exchangeId,
            shipping_address: shippingAddress,
            contact_name: contactName,
            contact_phone: contactPhone,
            csrfmiddlewaretoken: '{{ csrf_token }}'
        },
        success: function(response) {
            if (response.success) {
                alert('收货信息更新成功');
                $('#editShippingModal').modal('hide');
                location.reload();
            } else {
                alert(response.message);
            }
        },
        error: function() {
            alert('更新失败，请重试');
        }
    });
});
</script>
{% endblock %}